package com.wedu.modules.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wedu.common.utils.PageUtils;
import com.wedu.common.utils.Query;

import com.wedu.modules.sys.dao.SysUserDao;
import com.wedu.modules.sys.entity.SysUserEntity;
import com.wedu.modules.test.dao.StudentScoreDao;
import com.wedu.modules.test.entity.StudentScoreEntity;
import com.wedu.modules.test.entity.dto.StudentScoreDTO;
import com.wedu.modules.test.service.StudentScoreService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 学生成绩表
 */
@Service("studentScoreService")
public class StudentScoreServiceImpl extends ServiceImpl<StudentScoreDao, StudentScoreEntity> implements StudentScoreService {
    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 查询分页
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long userId) {
        String userName = (String)params.get("studentName");
        String examTime = (String)params.get("examTime");
        String minValue = (String)params.get("minValue");
        String maxValue = (String)params.get("maxValue");
        String isAsc = (String) params.get("isAsc");  // 获取排序方式
       String pageIndex = (String)params.get("pageIndex");
        String pageSize = (String)params.get("pageSize");
        SysUserEntity user = sysUserDao.selectById(userId); //根据当前用户id得到用户信息
        Integer student = user.getIsStudent(); //得到是否为学生的判断

        MPJLambdaWrapper<StudentScoreEntity> userMPJLambdaWrapper = JoinWrappers.lambda(StudentScoreEntity.class)
                .selectAll(StudentScoreEntity.class)
                .select(SysUserEntity::getRealName)
                .select(SysUserEntity::getIsStudent)
                .leftJoin(SysUserEntity.class, SysUserEntity::getUserId, StudentScoreEntity::getUserId)
               .like(StringUtils.isNotBlank(userName),"real_name", userName)
                .like(StringUtils.isNotBlank(examTime),"exam_time", examTime)
                .ge(StringUtils.isNotBlank(minValue), StudentScoreEntity::getMaths, minValue)  // 数学成绩大于等于最小值
                .le(StringUtils.isNotBlank(maxValue), StudentScoreEntity::getMaths, maxValue)  // 数学成绩小于等于最大值
                .eq("is_deleted", 0)
                .eq("is_student",1);
// 分页参数处理

// 执行分页查询


        // 判断排序方式，默认按升序排序
        if ("desc".equalsIgnoreCase(isAsc)) {
            userMPJLambdaWrapper.orderByDesc("exam_time");  // 降序排序
        } else {
            userMPJLambdaWrapper.orderByAsc("exam_time");  // 升序排序
        }
        IPage<StudentScoreEntity> page = this.page(//获取学生成绩表的分页信息
                new Query<StudentScoreEntity>().getPage(params),
                new QueryWrapper<StudentScoreEntity>()

        );
        IPage<StudentScoreDTO> studentPage = new Page<>();
        if(student==1){ //是学生，只查询当前学生的成绩
            userMPJLambdaWrapper.eq(StudentScoreEntity::getUserId,userId);

        }

        studentPage.setCurrent(page.getCurrent());
        studentPage.setPages(page.getPages());
        studentPage.setSize(page.getSize());
        IPage<StudentScoreDTO> result = baseMapper.selectJoinPage(studentPage, StudentScoreDTO.class, userMPJLambdaWrapper);
        List<StudentScoreDTO> studentList = baseMapper.selectJoinList(StudentScoreDTO.class, userMPJLambdaWrapper);
        page.setTotal(studentList.size());
        studentPage.setTotal(page.getTotal());

       // studentPage.setRecords(studentList);

        return new PageUtils(result);
    }


    /**
     * 查询学生列表
     */
    @Override
    public List<SysUserEntity> queryStudentList() {
        QueryWrapper<SysUserEntity>  userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("is_student", 1);//查询是学生的用户信息
        List<SysUserEntity> studentList = sysUserDao.selectList(userQueryWrapper);
        return studentList;
    }


}
